// pages/agent/index.js
const { agentPage } =  require('../../http/api')
const { agreements } =  require('../../http/api')
const { dailiMsg } =  require('../../http/api')
const {
  baseUrl
} = require('../../http/env').prod
var timer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePath:'',
    active:true,
    tabsActive:0,
    name:'', //，名称
    phone:'', //号码
    address:'', //地址
    master:'', //法人
    agentNo:'', //代理商编号
    tempFilePaths:'', //相片
    all:'',
    reject:'',
    today:'',
    contents:'',
    showOneButtonDialog:false, //弹窗
    oneButton:[{text: ''}],
    reason:'',
    status:0,
    time: 10,
    formData: {

    },
    rules: [{
      name: 'name',
      rules: {required: true, message: '公司名称是必选项'},
    }]
  },
  
  onLoad: function () {
    this.getAggremest()
    this.getDatas()
  },
   //倒计十秒
   clock(num) {
    var _this = this
    timer = setTimeout(function () {
      num--
      // console.log(num)
      if (num == -1) {
        clearTimeout(timer)
        _this.setData({
          oneButton: [{
            text:'确定'
          }],
          time:num,
        })
        // console.log(_this.data.time)
      } else {
        _this.setData({
          oneButton: [{
            text:'请先阅读'+ num + '秒'
          }]
        })
        // console.log('--timer--',num)
        _this.clock(num)
      }
    }, 1000)
  },
  getDatas:function(){
    dailiMsg().then(res=>{
      if(res.code==200){
        this.setData({
          name:res.data.company_name,
          phone:res.data.tel, //号码
          address:res.data.address, //地址
          master:res.data.user_name, //法人
          agentNo:res.data.re_code, //代理商编号
          tempFilePaths:baseUrl + res.data.shop_img,
          reason:res.data.reason,
          status:res.data.status
        })
      }
   })
  },
  // 上传
  upload(){
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        _this.setData({
          tempFilePaths:res.tempFilePaths
        })
        const url = _this.data.tempFilePaths
        wx.uploadFile({
          url:baseUrl + '/api/index/upload', 
          filePath: url[0],
          name: 'file',
          header: {
            'content-type': 'multipart/form-data'
          },
          success: res=>{
            _this.setData({
              tempFilePath:res.data
            })
          },
        })
        
      }
    })
  },

  // 输入框内容
  formInputChange(e){
    const field = e.currentTarget.dataset.field
    const val = e.detail.value
    console.log(field,val)
    if(field == 'name'){
      this.setData({
        name:val
      })
    }else if(field == 'phone' ){
      this.setData({
        phone:val
      })
    }else if(field == 'address'){
      this.setData({
        address:val
      })
    }else if(field == 'master'){
      this.setData({
        master:val
      })
    }else{
      this.setData({
        agentNo:val
      })
    }
  },
  //提交按钮
  tapOneDialogButton(){
    // console.log(111)
    var obj = {
      company_name:this.data.name,
      tel:this.data.phone,
      address:this.data.address,
      user_name:this.data.master,
      shop_img:this.data.tempFilePath,
      re_code:this.data.agentNo
    }
    // console.log(obj)
    agentPage(obj).then(res=>{
      if(res.code==200){
        var num = this.data.time
        this.setData({
          showOneButtonDialog:true
        })
        this.clock(num)
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none",
          duration:1500
        })
      }
    })
  },
  
  //跳转
  rejectTap(){
    wx.navigateTo({
      url: '../Rejectlist/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
// 商户分类
getAggremest(){
  const id = {
    id:1
  }
   agreements(id).then(res=>{
     this.setData({
      contents:res.data.content,
      // name:res.data.name
     })
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //确定按钮
  tapDialogButton(){
    const num = this.data.time
    console.log(num)
    if(num <1){
      wx.navigateTo({
        url: '../signature/index?',
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})